﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Grain.Crm.DataMigrator
{
    public static class Extensions
    {
        public static string GetByHeading(this string contentRow, string headerRow, string displayName)
        {
            if(!headerRow.Contains(displayName))
                throw new ArgumentException("Display name not found in headerRow");

            var headers = headerRow.Split(',');
            int index = 0;

            foreach(var header in headers)
            {
                if (header.TrimEnd().TrimStart() == displayName)
                    break;

                index++;
            }

            return contentRow.Split(',')[index];
        }

        public static string RemoveByHeading(this string contentRow, string headerRow, string displayName)
        {
            if(!headerRow.Contains(displayName))
                throw new ArgumentException("Display name not found in headerRow");

            var resultSB = new StringBuilder();

            var headers = headerRow.Split(',');
            int index = 0;

            foreach (var header in headers)
            {
                if (index > 0)
                    resultSB.Append(",");

                if (header.TrimEnd().TrimStart() != displayName)
                {
                    resultSB.Append(contentRow.Split(',')[index]);
                }

                index++;
            }

            return resultSB.ToString();
        }

        public static void Add<T>(this IList<T> list, IEnumerable<T> range)
        {
            foreach(var item in range)
                list.Add(item);
        }
    }
}
